import type { LoginResult, userInfoResult } from '@/types/member'
import { http } from '@/utils/http'

type LoginWxMinParams = {
  code: string
  encryptedData?: string
  iv?: string
}

type loginGetOpenIdRes = {
  openid: string
  code: number
  msg: string
}

/**
 * 获取用户opendid
 * @param code 登录时获取的 code
 */
export const getLoginWxMinOpenidAPI = (code: string) => {
  return http<loginGetOpenIdRes>({
    method: 'POST',
    url: '/app/code',
    data: {
      code,
    },
  })
}

/**
 * 小程序登录
 * @param data 请求参数
 */
export const postLoginWxMinAPI = (data: LoginWxMinParams) => {
  return http<LoginResult>({
    method: 'POST',
    url: '/login/wxMin',
    data,
  })
}
/**
 * 获取用户信息
 */
export const getUserInfoAPI = () => {
  return http<userInfoResult>({
    method: 'GET',
    url: 'app/userInfo',
  })
}

/**
 * 小程序登录_内测版
 * @param phoneNumber 模拟手机号码
 */
// export const postLoginWxMinSimpleAPI = (phoneNumber: string) => {
//   return http<LoginResult>({
//     method: 'POST',
//     url: '/login/wxMin/simple',
//     data: {
//       phoneNumber,
//     },
//   })
// }

type LoginParams = {
  account: string
  password: string
}
/**
 * 传统登录-用户名+密码
 * @param data 请求参数
 */
export const postLoginAPI = (data: LoginParams) => {
  return http<LoginResult>({
    method: 'POST',
    url: '/app/login',
    data,
  })
}
